package cn.edu.xmu.restfuldemo.service;


import cn.edu.xmu.restfuldemo.dao.ProductsDao;
import cn.edu.xmu.restfuldemo.model.*;
import cn.edu.xmu.restfuldemo.util.ResponseCode;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductsService {

    private Logger logger = LoggerFactory.getLogger(ProductsService.class);

    @Autowired
    private ProductsDao productsDao;

    /**
     * 获取某个货品信息，仅展示相关内容
     *
     * @param id 货品id
     * @return 货品对象
     */

    public Goods findGoodById(Integer id){ //return的都是BO
        Goods retGoodsBo = productsDao.findGoodById(id);
        if(retGoodsBo!=null)
        return retGoodsBo;
        else
            return null;
    }

    public ReturnObject<VoObject> findById(Integer id) {
        ReturnObject<Product> returnObject = productsDao.findProductById(id);
        ReturnObject<VoObject> retProductVo = null;
        if (returnObject.getCode().equals(ResponseCode.OK)) {
            if (returnObject.getData().getProductPo() != null) {
                retProductVo = new ReturnObject<>(returnObject.getData());
            }else{
                retProductVo = new ReturnObject<>(ResponseCode.RESOURCE_ID_NOTEXIST, returnObject.getErrmsg());
            }
        }else{
            retProductVo = new ReturnObject<>(returnObject.getCode(), returnObject.getErrmsg());
        }
        return retProductVo;
    }

}
